import {api} from './libs/api.js';
let _self;
export const index =  {
	data() {
		return {
			optLock:false,
			formData:{"mark":"","products":[]},
			rules:api.rules,
			products:[],
			salerShow:false,
			salerItem:api.salerItem,
			salerInfo:{},
			numMap:{},
			params:{}
		}
	},
	
	mounted() {
		_self = this;
	},
	onReachBottom:function(e){
		/*this.params.page+=1;
		this.initData();*/
	},
	onLoad:function(option) {
		//this.tabSubItem=option.status;
		this.params=option;
	},
	
	methods: {
		async initData(){
			//console.log(this.member);
			this.salerInfo={
				"user_name":this.member["user_name"],
				"tel":this.member["tel"],
				"user_code":this.member["user_code"],
			}
			
			if(this.products.length==0){
				let retInfo=await api.scanCodeInfo(this.params);
				if(retInfo["product"]) this.products.push(retInfo["product"]);		
			}
		},
		
		
		/**
		 * 价格数量计算
		 * @param {Object} index
		 */
		computeData(index){
			setTimeout(function(){
				let item=_self.products[index];
				let iQuantity=_self.products[index]["iQuantity"];
				if(isNaN(Number(iQuantity))) iQuantity=0;	
				_self.products[index]["iQuantity"]=iQuantity;

			},500);　			
		},
		

		delProduct(index){
			if(this.products.length==1){
				uni.showToast({icon:"none",title:"至少保留一条记录！"});
				return false;
			}
			this.products.splice(index,1);
		},
		
		
		/**
		* 扫码事件
		*/
		scanCodeEvent: function(){
			var that = this;
			uni.scanCode({
				success: async function (res) {
					let scanInfo=await api.scanCodeInfo({"code":res.result});
					
					if(!scanInfo["product"]){
						uni.showToast({icon:"none",title:"扫码无效！"});
					
					}else{
						let isExis=false;
						for(let index in that.products){
							let item=that.products[index];
							if(item["cInvCode"]==scanInfo["product"]["cInvCode"]) isExis=true;
						}
						if(isExis){
							uni.showToast({icon:"none",title:"产品重复！"});
						}else{
							that.products.push(scanInfo["product"]);
						}
					}
				}
			});
		},
		
		
		
		async submitForm(){
			if(this.optLock) return true;
			this.optLock=true;
			this.formData["saler"]=this.salerInfo;
			this.formData["products"]=this.products;			
			let ret=await api.addInventory(this.formData);
			if(ret=="success"){
				uni.showToast({icon:"none",title:"提交成功！"});　　
			}
			setTimeout(function(){uni.switchTab({url: "/pages/product/index"});},2000);
		}
				
	},
}